package medusa

import (
    //"flag"
    //"fmt"
    gocfg "github.com/robfig/config"
)

//////////////////////////////////////////////////////////////
//
type CfgFile struct {
    cfg     *gocfg.Config
    heading string
}

func NewCfgFile(filename string, heading string) *CfgFile {
    obj := new(CfgFile)
    obj.Constructor(filename, heading)
    return obj
}

func (self *CfgFile) Constructor(filename string, heading string) {
    self.cfg, _ = gocfg.ReadDefault(filename)
    self.heading = heading
}

/*
func (self *CfgFile) Load(filename string, heading string) {

}*/

func (self *CfgFile) ReadString(field string) string {
    val, _ := self.cfg.String(self.heading, field)
    return val
}

func (self *CfgFile) ReadInt(field string) int {
    val, _ := self.cfg.Int(self.heading, field)
    return val
}

func (self *CfgFile) ReadInt8(field string) int8 {
    return (int8)(self.ReadInt(field))
}

func (self *CfgFile) ReadInt16(field string) int16 {
    return (int16)(self.ReadInt(field))
}

func (self *CfgFile) ReadInt32(field string) int32 {
    return (int32)(self.ReadInt(field))
}

func (self *CfgFile) ReadInt64(field string) int64 {
    return (int64)(self.ReadInt(field))
}

func (self *CfgFile) ReadUint(field string) uint {
    return (uint)(self.ReadInt(field))
}

func (self *CfgFile) ReadUint8(field string) uint8 {
    return (uint8)(self.ReadInt(field))
}

func (self *CfgFile) ReadUint16(field string) uint16 {
    return (uint16)(self.ReadInt(field))
}

func (self *CfgFile) ReadUint32(field string) uint32 {
    return (uint32)(self.ReadInt(field))
}

func (self *CfgFile) ReadUint64(field string) uint64 {
    return (uint64)(self.ReadInt(field))
}

//////////////////////////////////////////////////////////////
//
type ListenCfg struct {
    CfgFile

    listenner_name string
    max_conn       int
    rcv_buf        int
    snd_buf        int
    snd_chan       int
    max_idle_sec   int64
}

func NewListenCfg(filename string, heading string) *ListenCfg {
    obj := new(ListenCfg)
    obj.Constructor(filename, heading)
    return obj
}

func (self *ListenCfg) Constructor(filename string, heading string) {
    self.listenner_name = self.ReadString("name")
    self.max_conn = self.ReadInt("max_conn")
    self.rcv_buf = self.ReadInt("rcv_buf")
    self.snd_buf = self.ReadInt("snd_buf")
    self.snd_chan = self.ReadInt("snd_chan")
    self.max_idle_sec = self.ReadInt64("idle")
}

/*
func (self *ListenCfg) Read(filename string, heading string) {

}*/

func (self *ListenCfg) GetListenerName() string {
    return self.listenner_name
}

func (self *ListenCfg) GetMaxConnNum() int {
    return self.max_conn
}

func (self *ListenCfg) GetRecvbufSize() int {
    return self.rcv_buf
}

func (self *ListenCfg) GetSendbufSize() int {
    return self.snd_buf
}

func (self *ListenCfg) GetSendChanNum() int {
    return self.snd_chan
}

func (self *ListenCfg) GetMaxIdle() int64 {
    return self.max_idle_sec
}
